/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vadung.contragame2d;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

/**
 *
 * @author dung.voanh
 */
public class SoundManger extends Thread{
    private static SoundManger s_soundManager;
    private static boolean s_isRunThread = false;
    private static boolean s_isPlay;
    private static int s_index;
    private static Player s_soundPlayer[];
    
    public static int s_iSound_Base = 0;    
    public static int s_iSound_fanfare = 1;
    public static int s_iSound_boss = 2;
    
    public static SoundManger GetInstance(){
        if(s_soundManager == null){
            s_soundManager = new SoundManger();
        }
        return s_soundManager;
    }

    private SoundManger() {
    }
    
    public  void Init(){
        int numSound = ResourceManager.s_aSoundRes.length;
        if(s_isRunThread || numSound <= 0){
            return;
        }
        
        s_isRunThread = true;
        s_isPlay = false;
        s_soundPlayer = new Player[numSound];
        
        LoadAllSound();
        
        try {
            this.start();
        } catch (Exception e) {
        }
        System.out.println("SoundManager's thread is start!");
    }
    
    private void LoadAllSound(){
        Load(s_iSound_Base);
        Load(s_iSound_fanfare);
        Load(s_iSound_boss);
    }
    
    private void Load(int soundID) {
        InputStream is = getClass().getResourceAsStream(ResourceManager.s_aSoundRes[soundID].toString());
        try {
            s_soundPlayer[soundID] = Manager.createPlayer(is, "audio/midi");

        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        } catch (MediaException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void PlaySound(int soundID) {
        if (!s_isRunThread) {
            return;
        }
        if (s_isPlay && soundID != s_index) {
            StopPlay(soundID);
        }
        s_index = soundID;
        s_isPlay = true;
    }
    
    private void StartPlay(int soundID) {
        try {
            if (s_isPlay == false) {
                return;
            }
            if (s_soundPlayer[soundID].getState() == Player.PREFETCHED) {
                s_soundPlayer[soundID].deallocate();
                s_soundPlayer[soundID].prefetch();
                s_soundPlayer[soundID].start();
            }
            if (s_soundPlayer[soundID].getState() != Player.STARTED) {
                s_soundPlayer[soundID].realize();
                s_soundPlayer[soundID].prefetch();                
                s_soundPlayer[soundID].start();
            }

        } catch (MediaException ex) {
            System.out.println("ERROR: SoundManager StartPlay(soundID)." );
            System.out.println(ex.getMessage());
        }
    }
    
    public void StopPlayAll() {
        for(int i = 0; i < s_soundPlayer.length; i++){
            StopPlay(i);
        }
    }
    
    public void StopPlay(int soundID) {
        if (!s_isRunThread) {
            return;
        }
        try {
            s_soundPlayer[soundID].stop();
            s_isPlay = false;
        } catch (MediaException ex) {
        }
    }

    public void run() {
        while(s_isRunThread){
            try {
                StartPlay(s_index);
            } catch (Exception e) {
            }
        }
    }
    
    public void Stop(){
        StopPlayAll();
//        s_isRunThread = false;
//        try {
//            this.join();
//        } catch (InterruptedException ex) {
//            //ex.printStackTrace();
//        }
    }
}
